package fu.k4c.vTeam.ViewDetails;

import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import com.google.android.maps.MapView;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import fu.k4c.vTeam.R;
import fu.k4c.vTeam.Controller.VlocationItemizedOverlay;
import fu.k4c.vTeam.Object.ItemImage;
import fu.k4c.vTeam.Object.LikeAndVisit;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

public class PlaceTabLayoutActivity extends TabActivity {
	Boolean visted;
	Boolean liked;
	String encoded;
	int report;
	Bundle data;
	String email;
	String fullName;
	int CURRENT_TAG = 0;
	static final int TAG_GUEST = 0;
	static final int TAG_USER = 1;
	private static final int DIALOG_REPORT = 1;
	ProgressDialog proDialog;
	private DisplayImageOptions options;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_layout_activity);
		Button btn_back = (Button) findViewById(R.id.btn_viewPlace_back);
		btn_back.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		data = getIntent().getExtras();
		overridePendingTransition(android.R.anim.fade_in,
				android.R.anim.fade_out);
		proDialog = ProgressDialog.show(PlaceTabLayoutActivity.this, "",
				"Đang tải dữ liệu...");
		new Thread() {
			public void run() {

				try {
					sleep(3000);

				} catch (Exception e) {
					System.out.print(e.toString());
				}
				// dismiss the progressdialog
				proDialog.dismiss();
			}
		}.start();
		options = new DisplayImageOptions.Builder()
				.showImageForEmptyUri(R.drawable.image_for_empty_url)
				.showStubImage(R.drawable.stub_image).cacheInMemory()
				.cacheOnDisc().build();
		try {
			SchemeRegistry registry = new SchemeRegistry();
			registry.register(new Scheme("http", PlainSocketFactory
					.getSocketFactory(), 80));
			HttpParams httpParams = new BasicHttpParams();
			ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager(
					httpParams, registry);
			HttpClient client = new DefaultHttpClient(connMgr, httpParams);
			HttpPost post = new HttpPost(getString(R.string.connectionString)
					+ "vLocationService/checklikeandvisit");
			post.setHeader("Accept", "application/json");
			post.setHeader("Content-type", "application/json");
			HashMap<String, Object> postParameters = new HashMap<String, Object>();
			postParameters.put("UserName", data.getString("username"));
			postParameters.put("PlaceId", data.getString("IDofItemRevision"));
			Gson gson = new Gson();
			String jsonParameters = gson.toJson(postParameters);
			post.setEntity(new StringEntity(jsonParameters));
			HttpResponse response = client.execute(post);
			HttpEntity entity = response.getEntity();
			String jsonresult = EntityUtils.toString(entity);
			LikeAndVisit fvr = gson.fromJson(jsonresult, LikeAndVisit.class);
			// Toast.makeText(getApplicationContext(),jsonresult,
			// Toast.LENGTH_LONG).show();
			fullName = fvr.getFullName();
			if (fvr.getLike().equals("true")) {
				liked = true;
			} else {
				liked = false;
			}
			if (fvr.getVisit().equals("true")) {
				visted = true;
			} else {
				visted = false;
			}
			if (fvr.getIsUser().equals("true")) {
				report = 2;
			} else if (fvr.getReport().equals("true")) {
				report = 1;
			} else if (fvr.getReport().equals("false")) {
				report = 0;
			}
		} catch (Exception e) {
			// TODO: handle exception
			// Toast.makeText(getApplicationContext(),"loi " + e.toString(),
			// Toast.LENGTH_LONG).show();
		}
		Resources res = getResources();
		TabHost tabHost = getTabHost();
		TabHost.TabSpec spec;
		Intent intent;

		intent = new Intent().setClass(this, InfoActivity.class);
		intent.putExtra("address", data.getString("address"));
		intent.putExtra("placename", data.getString("placename"));
		intent.putExtra("placetel", data.getString("placetel"));
		intent.putExtra("likenumber", data.getInt("likenumber"));
		intent.putExtra("visitnumber", data.getInt("visitnumber"));
		intent.putExtra("createby", data.getString("createby"));
		intent.putExtra("des", data.getString("des"));
		intent.putExtra("email", data.getString("username"));
		intent.putExtra("liked", liked);
		intent.putExtra("visited", visted);
		// intent.putExtra("lat", data.getString("lat"));
		// intent.putExtra("long", data.getString("long"));
		intent.putExtra("IDofItemRevision", data.getString("IDofItemRevision"));
		intent.putExtra("views", "" + data.getString("views"));
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		spec = tabHost
				.newTabSpec("info")
				.setIndicator("Thông tin chi tiết",
						res.getDrawable(R.drawable.info)).setContent(intent);
		tabHost.addTab(spec);
		// Tab anh o day
		intent = new Intent().setClass(this, ImageActivity.class);
		intent.putExtra("email", data.getString("username"));
		intent.putExtra("IDofItemRevision", data.getString("IDofItemRevision"));
		intent.putExtra("createby", data.getString("createbyemail"));
		spec = tabHost.newTabSpec("image")
				.setIndicator("Hình ảnh", res.getDrawable(R.drawable.img))
				.setContent(intent);
		tabHost.addTab(spec);
		// //////////////////////////////////////////
		intent = new Intent().setClass(this, MapViewActivity.class);
		intent.putExtra("email", data.getString("username"));
		intent.putExtra("report", report);
		intent.putExtra("lat", data.getString("lat"));
		intent.putExtra("long", data.getString("long"));
		intent.putExtra("IDofItemRevision", data.getString("IDofItemRevision"));
		spec = tabHost.newTabSpec("maps")
				.setIndicator("Bản đồ", res.getDrawable(R.drawable.map))
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, CommentActivity.class);
		intent.putExtra("email", data.getString("username"));
		intent.putExtra("IDofItemRevision", data.getString("IDofItemRevision"));
		intent.putExtra("fullName", fullName);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		spec = tabHost.newTabSpec("comment")
				.setIndicator("Bình luận", res.getDrawable(R.drawable.comment))
				.setContent(intent);
		tabHost.addTab(spec);

		tabHost.setCurrentTab(0);

	}
}